package com.zhwykj.greendao2020.ui;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.zhwykj.greendao2020.R;
import com.zhwykj.greendao2020.app.BaseApplication;
import com.zhwykj.greendao2020.bean.Book;
import com.zhwykj.greendao2020.bean.BookDao;

import java.util.List;

public class BookActivity extends AppCompatActivity {
    private Button btnAdd, btnDelete, btnUpdate, btnQuery;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book);

        initView();
        initListener();
    }

    private void initView() {
        btnAdd = findViewById(R.id.btn_book_add);
        btnDelete = findViewById(R.id.btn_book_delete);
        btnUpdate = findViewById(R.id.btn_book_update);
        btnQuery = findViewById(R.id.btn_book_query);
    }

    private void initListener() {
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Book book1 = new Book();
                book1.setAuthor("郭霖");
                book1.setPrice(79.00);
                book1.setPages(570);
                book1.setName("第一行代码");
                book1.setDate("2020-03-13");
                book1.setWriteTime(System.currentTimeMillis());

                BaseApplication.getDaoInstant().getBookDao().insert(book1);
            }
        });
        
        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BaseApplication.getDaoInstant().getBookDao().queryBuilder().where(BookDao.Properties.Author.eq("郭霖")).buildDelete().executeDeleteWithoutDetachingEntities();
                //BaseApplication.getDaoInstant().getBookDao().deleteAll();
            }
        });
        
        btnUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<Book> books = BaseApplication.getDaoInstant().getBookDao().queryBuilder().where(BookDao.Properties.Author.eq("郭霖")).list();
                for (int i = 0; i < books.size(); i++) {
                    books.get(i).setName("第二行代码");
                }
                BaseApplication.getDaoInstant().getBookDao().updateInTx(books);
            }
        });

        btnQuery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<Book> books = BaseApplication.getDaoInstant().getBookDao().queryBuilder().list();
                for (int i = 0; i < books.size(); i++) {
                    Log.e("book" + i, books.get(i).getId() + "/"
                            + books.get(i).getAuthor() + "/"
                            + books.get(i).getPrice() + "/"
                            + books.get(i).getPages() + "/"
                            + books.get(i).getName() + "/"
                            + books.get(i).getDate() + "/"
                            + books.get(i).getWriteTime());
                }
            }
        });
    }
}
